import pygame
import sys
import random
import math
from pygame import gfxdraw

# 初始化pygame
pygame.init()
pygame.font.init()

# 设置窗口
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("儿童节快乐！")

# 颜色定义
BACKGROUND = (230, 250, 255)
COLORS = [
    (255, 105, 180),  # 热粉红
    (255, 215, 0),  # 金色
    (50, 205, 50),  # 酸橙绿
    (30, 144, 255),  # 道奇蓝
    (255, 165, 0),  # 橙色
    (138, 43, 226),  # 紫罗兰
    (255, 69, 0),  # 红橙色
]
FONT_COLOR = (40, 40, 40)

# 加载字体 - 使用支持字母的字体
title_font = pygame.font.SysFont("simhei", 60, bold=True)
name_font = pygame.font.SysFont("simhei", 48)
button_font = pygame.font.SysFont("simhei", 32)
message_font = pygame.font.SysFont("simhei", 28)
input_font = pygame.font.SysFont("simhei", 32)  # 专门用于输入框的字体，支持字母

# 祝福语列表
MESSAGES = [
    "愿你永远保持童真，快乐成长！",
    "儿童节快乐，愿你天天开心！",
    "愿你像小星星一样闪闪发光！",
    "保持好奇心，探索世界的奇妙！",
    "愿你梦想成真，快乐每一天！",
    "童年是彩虹，愿你的生活五彩斑斓！",
    "像小鸟一样自由飞翔，快乐成长！"
]


# 气球类
class Balloon:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = random.randint(50, WIDTH - 50)
        self.y = HEIGHT + 50
        self.speed = random.uniform(0.5, 2.0)
        self.color = random.choice(COLORS)
        self.radius = random.randint(20, 40)
        self.string_length = random.randint(80, 150)
        self.swing_angle = random.uniform(0, 2 * math.pi)
        self.swing_speed = random.uniform(0.02, 0.05)
        self.swing_range = random.uniform(5, 15)

    def update(self):
        self.y -= self.speed
        self.swing_angle += self.swing_speed
        self.x += math.sin(self.swing_angle) * self.swing_range

        if self.y < -100:
            self.reset()

    def draw(self, surface):
        # 绘制气球
        pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)

        # 绘制高光
        highlight_pos = (int(self.x - self.radius * 0.3), int(self.y - self.radius * 0.3))
        pygame.draw.circle(surface, (255, 255, 255, 150), highlight_pos, int(self.radius * 0.3))

        # 绘制绳子
        string_end_x = self.x + math.sin(self.swing_angle) * self.string_length * 0.3
        string_end_y = self.y + self.string_length
        pygame.draw.line(surface, (200, 200, 200), (self.x, self.y + self.radius),
                         (string_end_x, string_end_y), 2)


# 星星类
class Star:
    def __init__(self):
        self.reset()

    def reset(self):
        self.x = random.randint(0, WIDTH)
        self.y = random.randint(0, HEIGHT // 2)
        self.size = random.randint(2, 5)
        self.speed = random.uniform(0.1, 0.5)
        self.twinkle_speed = random.uniform(0.02, 0.1)
        self.alpha = random.randint(100, 255)
        self.twinkle_dir = 1

    def update(self):
        self.alpha += self.twinkle_speed * self.twinkle_dir
        if self.alpha > 255:
            self.alpha = 255
            self.twinkle_dir = -1
        elif self.alpha < 100:
            self.alpha = 100
            self.twinkle_dir = 1

    def draw(self, surface):
        color = (255, 255, 255, int(self.alpha))
        pygame.draw.circle(surface, color, (int(self.x), int(self.y)), self.size)


# 按钮类
class Button:
    def __init__(self, x, y, width, height, text, color):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = color
        self.hover_color = (min(color[0] + 30, 255), min(color[1] + 30, 255), min(color[2] + 30, 255))
        self.current_color = color

    def draw(self, surface):
        pygame.draw.rect(surface, self.current_color, self.rect, border_radius=15)
        pygame.draw.rect(surface, (100, 100, 100), self.rect, 3, border_radius=15)

        text_surf = button_font.render(self.text, True, FONT_COLOR)
        text_rect = text_surf.get_rect(center=self.rect.center)
        surface.blit(text_surf, text_rect)

    def is_hovered(self, pos):
        if self.rect.collidepoint(pos):
            self.current_color = self.hover_color
            return True
        self.current_color = self.color
        return False


# 创建气球
balloons = [Balloon() for _ in range(10)]
stars = [Star() for _ in range(50)]

# 创建按钮
color_buttons = []
for i in range(7):
    x = 100 + i * 80
    y = 180
    color_buttons.append(Button(x, y, 60, 60, "", COLORS[i]))

name_button = Button(WIDTH // 2 - 100, 300, 200, 60, "输入名字", (200, 230, 255))
submit_button = Button(WIDTH // 2 - 100, 350, 200, 50, "提交名字", (180, 200, 255))  # 新增提交按钮
generate_button = Button(WIDTH // 2 - 100, 420, 200, 60, "生成贺卡", (180, 255, 180))

# 输入框
input_box = pygame.Rect(WIDTH // 2 - 100, 260, 200, 40)
input_text = ""
input_active = False

# 当前选择
selected_color = COLORS[0]
child_name = ""
card_generated = False
current_message = random.choice(MESSAGES)

# 主循环
clock = pygame.time.Clock()
running = True

while running:
    mouse_pos = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # 检查按钮点击
            if not card_generated:
                for i, button in enumerate(color_buttons):
                    if button.is_hovered(mouse_pos):
                        selected_color = COLORS[i]

                if name_button.is_hovered(mouse_pos):
                    input_active = True
                    input_text = ""

                # 新增：提交按钮处理
                if submit_button.is_hovered(mouse_pos):
                    if input_text.strip():
                        child_name = input_text.strip()
                        input_active = False

                if generate_button.is_hovered(mouse_pos) and child_name:
                    card_generated = True
                    current_message = random.choice(MESSAGES)

            # 检查输入框点击
            if input_box.collidepoint(event.pos):
                input_active = True
                input_text = ""
            else:
                input_active = False

            # 托管TEXTINPUT事件，用于输入文本

        elif event.type == pygame.TEXTINPUT and input_active:

            if len(input_text) < 15:  # 限制输入长度

                input_text += event.text


        elif event.type == pygame.KEYDOWN and input_active:

            if event.key == pygame.K_RETURN:

                if input_text.strip():
                    child_name = input_text.strip()

                input_active = False

            elif event.key == pygame.K_BACKSPACE:

                input_text = input_text[:-1]

    # 更新
    for balloon in balloons:
        balloon.update()

    for star in stars:
        star.update()

    # 绘制
    screen.fill(BACKGROUND)

    # 绘制背景元素
    for star in stars:
        star.draw(screen)

    for balloon in balloons:
        balloon.draw(screen)

    # 绘制云朵
    for i in range(3):
        x = 150 + i * 200
        y = 80
        pygame.draw.circle(screen, (250, 250, 250), (x, y), 30)
        pygame.draw.circle(screen, (250, 250, 250), (x - 20, y + 10), 25)
        pygame.draw.circle(screen, (250, 250, 250), (x + 20, y + 10), 25)
        pygame.draw.circle(screen, (250, 250, 250), (x - 40, y), 20)
        pygame.draw.circle(screen, (250, 250, 250), (x + 40, y), 20)

    # 绘制彩虹
    rainbow_colors = [(255, 0, 0), (255, 165, 0), (255, 255, 0),
                      (0, 255, 0), (0, 0, 255), (75, 0, 130), (148, 0, 211)]
    center_x, center_y = WIDTH // 2, HEIGHT + 50
    for i, color in enumerate(rainbow_colors):
        radius = 250 - i * 20
        pygame.draw.circle(screen, color, (center_x, center_y), radius, 10)

    if not card_generated:
        # 绘制标题
        title_surf = title_font.render("儿童节定制贺卡", True, FONT_COLOR)
        screen.blit(title_surf, (WIDTH // 2 - title_surf.get_width() // 2, 50))

        # 绘制说明
        instruction = button_font.render("请选择颜色并输入名字:", True, FONT_COLOR)
        screen.blit(instruction, (WIDTH // 2 - instruction.get_width() // 2, 140))

        # 绘制颜色按钮
        for button in color_buttons:
            button.is_hovered(mouse_pos)
            button.draw(screen)

        # 绘制选中的颜色预览
        pygame.draw.circle(screen, selected_color, (WIDTH // 2, 220), 25)
        pygame.draw.circle(screen, (0, 0, 0), (WIDTH // 2, 220), 25, 2)

        # 绘制输入框
        pygame.draw.rect(screen, (255, 255, 255), input_box, border_radius=10)
        pygame.draw.rect(screen, (100, 100, 100), input_box, 2, border_radius=10)

        # 使用支持字母的字体渲染输入文本
        name_surf = input_font.render(input_text, True, FONT_COLOR)
        screen.blit(name_surf, (input_box.x + 5, input_box.y + 5))

        if input_active:
            pygame.draw.rect(screen, (30, 144, 255), input_box, 2, border_radius=10)

            # 绘制输入光标
            if pygame.time.get_ticks() % 1000 < 500:
                cursor_x = input_box.x + 5 + name_surf.get_width()
                pygame.draw.line(screen, FONT_COLOR,
                                 (cursor_x, input_box.y + 5),
                                 (cursor_x, input_box.y + input_box.height - 5), 2)

        # 显示输入提示
        prompt = button_font.render("请输入名字:", True, FONT_COLOR)
        screen.blit(prompt, (input_box.x, input_box.y - 30))

        # 绘制按钮
        name_button.is_hovered(mouse_pos)
        name_button.draw(screen)

        # 新增：绘制提交按钮
        submit_button.is_hovered(mouse_pos)
        submit_button.draw(screen)

        if child_name:
            generate_button.is_hovered(mouse_pos)
            generate_button.draw(screen)

            # 显示已输入的名字
            name_display = button_font.render(f"已输入: {child_name}", True, (30, 144, 255))
            screen.blit(name_display, (WIDTH // 2 - name_display.get_width() // 2, 500))
    else:
        # 绘制贺卡
        # 绘制标题
        title_surf = title_font.render("儿童节快乐！", True, selected_color)
        screen.blit(title_surf, (WIDTH // 2 - title_surf.get_width() // 2, 80))

        # 绘制名字
        name_surf = name_font.render(f"亲爱的 {child_name}", True, FONT_COLOR)
        screen.blit(name_surf, (WIDTH // 2 - name_surf.get_width() // 2, 170))

        # 绘制祝福语
        message_surf = message_font.render(current_message, True, FONT_COLOR)
        screen.blit(message_surf, (WIDTH // 2 - message_surf.get_width() // 2, 250))

        # 绘制蛋糕
        pygame.draw.rect(screen, (255, 220, 120), (WIDTH // 2 - 80, 320, 160, 80))
        pygame.draw.rect(screen, (255, 150, 150), (WIDTH // 2 - 70, 300, 140, 30))

        # 绘制蜡烛
        for i in range(5):
            x = WIDTH // 2 - 60 + i * 30
            pygame.draw.rect(screen, selected_color, (x, 270, 5, 30))
            # 绘制火焰
            pygame.draw.circle(screen, (255, 255, 0), (x + 2, 260), 6)

        # 绘制礼物盒
        pygame.draw.rect(screen, (255, 100, 100), (150, 320, 80, 60))
        pygame.draw.rect(screen, (255, 255, 255), (150, 320, 80, 60), 2)
        pygame.draw.rect(screen, (255, 255, 255), (185, 300, 10, 20))
        pygame.draw.rect(screen, (255, 255, 255), (165, 345, 50, 10))

        pygame.draw.rect(screen, (100, 200, 100), (WIDTH - 230, 320, 80, 60))
        pygame.draw.rect(screen, (255, 255, 255), (WIDTH - 230, 320, 80, 60), 2)
        pygame.draw.rect(screen, (255, 255, 255), (WIDTH - 195, 300, 10, 20))
        pygame.draw.rect(screen, (255, 255, 255), (WIDTH - 215, 345, 50, 10))

        # 绘制重新生成按钮
        regenerate_button = Button(WIDTH // 2 - 100, 450, 200, 50, "重新生成", (180, 200, 255))
        if regenerate_button.is_hovered(mouse_pos):
            if pygame.mouse.get_pressed()[0]:
                card_generated = False
        regenerate_button.draw(screen)

        # 绘制结束语
        end_text = message_font.render("祝你有一个快乐的儿童节！", True, (100, 100, 200))
        screen.blit(end_text, (WIDTH // 2 - end_text.get_width() // 2, 520))

    pygame.display.flip()
    clock.tick(60)

pygame.quit()
sys.exit()